package com.example.widgetcase.slice;

import com.example.widget_lib.widget.GradientLine;
import com.example.widgetcase.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;

import java.util.concurrent.atomic.AtomicBoolean;

public class GradientLineAbility extends BaseSlice {

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        GradientLine line1 = (GradientLine) findComponentById(ResourceTable.Id_gradientline1);
        GradientLine line2 = (GradientLine) findComponentById(ResourceTable.Id_gradientline2);
        GradientLine line3 = (GradientLine) findComponentById(ResourceTable.Id_gradientline3);
        GradientLine line4 = (GradientLine) findComponentById(ResourceTable.Id_gradientline4);
        Button button1 = (Button) findComponentById(ResourceTable.Id_button1);
        Button button2 = (Button) findComponentById(ResourceTable.Id_button2);

        getUITaskDispatcher().delayDispatch(() -> {
            line1.setAnim(true);
            line1.setMode(GradientLine.LineMode.DOWN);
        }, 500);

        line2.setAnim(true);

        getUITaskDispatcher().delayDispatch(() -> line2.setMode(GradientLine.LineMode.FLAT), 500);

        getUITaskDispatcher().delayDispatch(() -> {
            line3.setAnim(true, 640);
            line3.setMode(GradientLine.LineMode.UP);
        }, 500);

        button1.setClickedListener(component -> {
            getUITaskDispatcher().delayDispatch(line1::handleStartAnim, 120);
            line2.handleStartAnim();
            getUITaskDispatcher().delayDispatch(line3::handleStartAnim, 120);
        });

        line4.setAnim(true, 640);
        line4.setMode(GradientLine.LineMode.FLAT);

        AtomicBoolean isTure = new AtomicBoolean(false);

        button2.setClickedListener(component -> {
            if (isTure.get()) {
                isTure.set(false);
                line4.setMode(GradientLine.LineMode.FLAT);
                line4.handleStartAnim();
            } else {
                isTure.set(true);
                line4.setMode(GradientLine.LineMode.UP);
                line4.handleStartAnim();
            }
        });

    }

    @Override
    public int getChildUIContent() {
        return ResourceTable.Layout_gradient_line;
    }
}
